package com.mapper;

import com.domain.BlogArticle;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BlogArticleMapper {
    @Select("SELECT * FROM t_article WHERE id=#{id}")
    public BlogArticle selectArticleWithId(Integer id);

    // 发表文章，同是获取使用@options注释获取自动生成主键id
    @Insert("INSERT INTO t_article (title,created,modified,tags,categories,allow_comment,thumbnail,content)" +
            "VALUES(#{title},#{created},#{modified},#{tags},#{categories},#{allowComment},#{thumbnail},#{content})")

    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    public Integer publishArticle(BlogArticle article);

    @Select("SELECT * FROM t_article ORDER BY id DESC")
    public List<BlogArticle> selectArticleWithPage();

//    @Delete("Delete * FROM t_article WHERE id=#{id}")
//    public void deleteArticleWithId(int id);

    @Select("SELECT COUNT(1) FROM t_article")
    public Integer countArticle();

//    @Update("UPDATE t_article SET title=#{title},created=#{created},modified=#{modified},tags=#{tags},thumbnail=#{thumbnail},content=#{content}" +
//            "categories=#{categories},hits=#{hits},comments_num=#{commentsNum},allow_comment=#{allowComment}WHERE id=#{id}")
//    public Integer updateArticleWithId(BlogArticle article);


    @Update({
            "<script>",
            "UPDATE t_article",
            "<set>",
            "<if test='title != null'>title = #{title},</if>",
            "<if test='content != null'>content = #{content},</if>",
            "<if test='created != null'> created = #{created},</if>",

            "<if test='categories != null'>  = #{categories},</if>",
            "<if test='tages != null'>  = #{tages},</if>",
            "<if test='allowComment != null'>  = #{allowComment},</if>",
            "<if test='thumbail != null'>  = #{thumbail},</if>",
            "modified = CURRENT_TIMESTAMP,"+
            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    public Integer updateArticleWithId(BlogArticle article);

    BlogArticle selectArticleWithId(int parseInt);

    void deleteArticleWithId(int id);
}
